<template>
    <view class="container" :class="{ paddingBottomBar: isShowBottomBar }">
        <!-- 			<view @click="DatePicker('datetime')">qweqw</view> -->
        <view class="pricemain">
            <view class="top from">
                <view class="title"
                    ><image
                        :src="
                            data.SettlementType == 0
                                ? '/pages_supplier/static/account1.png'
                                : '/pages_supplier/static/account2.png'
                        "></image
                    >{{ data.StateName }}金额</view
                >
                <view class="price"><view>￥</view>{{ data.PriceSum }}</view>
            </view>
            <view class="row">
                <view
                    ><view class="row-label">结算类型：</view
                    >{{ data.SettlementType == 0 ? "采购结算" : "退货结算" }}</view
                >
                <view v-if="data.State == 2 || data.State == 3 || data.State == 5"
                    ><view class="row-label">支付方式：</view>线下支付</view
                >
                <view v-if="(data.State == 5 || data.State == 4) && data.CheckMsg"
                    ><view class="row-label">驳回原因：</view><view style="flex: 1">{{ data.CheckMsg }}</view></view
                >
                <view v-if="data.State == 0"><view class="row-label">发起时间：</view>{{ data.AddTime }}</view>
                <view v-if="data.State == 0 && data.FileUrl">
                    <view class="row-label">附件：</view>
                    <view class="imglist" v-if="accessoryShow">
                        <image
                            class="img"
                            v-for="img in easyImgList(data.FileUrl)"
                            :src="easyGetImgUrl(img)"
                            :data-src="easyGetImgUrl(img)"
                            @tap="previewImage"
                            mode="aspectFill"></image>
                    </view>
                    <view v-else>含有其他附件，请前往PC端查看</view>
                </view>
                <view v-if="data.State == 0 && data.Remark"
                    ><view class="row-label">备注：</view>{{ data.Remark }}</view
                >
                <view
                    v-if="(data.State == 5 || data.State == 1) && data.SettlementType == 1"
                    @click="DatePicker('datetime')">
                    <view class="row-label">付款时间：</view>
                    <view style="flex: 1" class="flex-between">
                        <view>{{ payDate }}</view>
                        <uni-icons type="arrowright" size="20" customIcons="icon-gray-1"></uni-icons>
                    </view>
                </view>
                <view v-if="(data.State == 5 || data.State == 1) && data.SettlementType == 1">
                    <view class="row-label">支付凭证：</view>
                    <view class="imgbox" style="flex: 1">
                        <img-item :data="upimg" @chooseImage="chooseImage" :num="9" :index="1"></img-item>
                        <text class="tips">支持gif,jpg,jpeg,bmg,png(最多可上传9张)</text>
                    </view>
                </view>
                <view v-if="(data.State == 2 || data.State == 3 || data.State == 4) && data.Imgs">
                    <view class="row-label">支付凭证：</view>
                    <view class="imglist">
                        <image
                            class="img"
                            v-for="img in easyImgList(data.Imgs)"
                            :src="easyGetImgUrl(img)"
                            :data-src="easyGetImgUrl(img)"
                            @tap="previewImage1"
                            mode="aspectFill"></image>
                    </view>
                </view>
            </view>
        </view>
        <view class="box">
            <view class="title">收款信息</view>
            <view class="row">
                <view><view class="row-label">接收方：</view>{{ data.AuditSupplierName }}</view>
                <view><view class="row-label">银行名称：</view>{{ data.BankName }}</view>
                <view><view class="row-label">银行账号：</view>{{ data.BankNumber }}</view>
                <view><view class="row-label">账户名称：</view>{{ data.AccountName }}</view>
            </view>
        </view>
        <view class="go flex-between" @click="gotolist">
            <view>{{ data.SettlementType == 0 ? "关联采购单" : "关联退货单" }}</view>
            <uni-icons type="arrowright" size="20" customIcons="icon-gray-1"></uni-icons>
        </view>
        <view
            class="box"
            v-if="data.State == 2 || data.State == 1 || data.State == 3 || data.State == 4 || data.State == 5">
            <view class="title">订单信息</view>
            <view class="row">
                <view v-if="data.State == 2 || data.State == 3 || data.State == 5"
                    ><view class="row-label">付款时间：</view>{{ data.PayTime }}</view
                >
                <view v-if="data.State == 1 || data.State == 2 || data.State == 3 || data.State == 4 || data.State == 5"
                    ><view class="row-label">发起时间：</view>{{ data.AddTime }}</view
                >
                <view v-if="data.FileUrl">
                    <view class="row-label">附件：</view>
                    <view class="imglist" v-if="accessoryShow">
                        <image
                            class="img"
                            v-for="img in easyImgList(data.FileUrl)"
                            :src="easyGetImgUrl(img)"
                            :data-src="easyGetImgUrl(img)"
                            @tap="previewImage"
                            mode="aspectFill"></image>
                    </view>
                    <view v-else>含有其他附件，请前往PC端查看</view>
                </view>
            </view>
        </view>
        <view class="footer" v-if="isShowBottomBar">
            <view
                v-if="data.SettlementType == 0 && data.State == 2 && data.IsConfirmPay == 1"
                class="btn btn-success-hollow"
                @click="reject"
                >驳回</view
            >
            <view
                v-if="data.SettlementType == 0 && data.State == 2 && data.IsConfirmPay == 1"
                class="btn btn-success"
                @click="payConfirm"
                >确认</view
            >
            <view v-if="data.SettlementType == 1 && data.IsPay == 1" class="btn btn-success" @click="submit">提交</view>
            <view
                v-if="data.SettlementType == 1 && data.State == 0 && data.IsCheck == 1"
                class="btn btn-success-hollow"
                @click="back"
                >退回</view
            >
            <view
                v-if="data.SettlementType == 1 && data.State == 0 && data.IsCheck == 1"
                class="btn btn-success"
                @click="agree"
                >同意</view
            >
        </view>
        <uni-popup ref="popRejectTip" type="center" :animation="false">
            <view class="pop-return-box">
                <view class="pop-return-title">
                    <text>结算驳回</text>
                </view>
                <view style="text-align: center">是否确定驳回该笔款项？</view>
                <textarea v-model="rejectDesc" placeholder="请输入驳回原因" />
                <view class="pop-action">
                    <view @click="rejectClose">取消</view>
                    <view @click="rejectConfirm">确定</view>
                </view>
            </view>
        </uni-popup>
        <uni-popup ref="popReturnTip" type="center" :animation="false">
            <view class="pop-return-box">
                <view class="pop-return-title">
                    <text>结算退回</text>
                </view>
                <view style="text-align: center">是否确定退回该笔款项？</view>
                <textarea v-model="desc" placeholder="请输入退回原因" />
                <view class="pop-action">
                    <view @click="close">取消</view>
                    <view @click="confirm">确定</view>
                </view>
            </view>
        </uni-popup>
        <uni-popup ref="popReturnTip1" type="center" :animation="false">
            <view class="pop-return-box">
                <view class="pop-return-title">
                    <text>结算审核</text>
                </view>
                <view class="desc">是否确定通过该笔款项？</view>
                <view class="pop-action">
                    <view @click="close1">取消</view>
                    <view @click="confirm1">确定</view>
                </view>
            </view>
        </uni-popup>
        <mx-date-picker
            :show="showPicker"
            :type="type"
            :value="value"
            :show-tips="true"
            :show-seconds="true"
            @confirm="ed"
            @cancel="ed" />
    </view>
</template>

<script>
import { mapMutations } from "vuex"
import {
    GetSettlementOrderById,
    ConfirmSettlementOrder,
    CheckSettlementOrder,
    PaySettlementOrder,
} from "@/pages_supplier/service/api/buyOrder.js"
import utils from "@/utils/utils.js"
import MxDatePicker from "@/components/mx-datepicker/mx-datepicker.vue"
import imgItem from "@/pages_supplier/components/form/img-item.vue"
export default {
    components: {
        MxDatePicker,
        imgItem,
    },
    data() {
        const defaultdaytime = utils.getDatetime({
            format: true,
        })
        return {
            data: {},
            desc: "",
            rejectDesc: "",
            type: "datetime",
            payDate: defaultdaytime,
            isShowBottomBar: false,
            id: "",
            value: "",
            upimg: "",
            showPicker: false,
            payimglist: [],
            accessoryShow: true,
        }
    },
    onLoad(e) {
        this.id = e.id
    },
    mounted() {
        this.getDetail()
        uni.setNavigationBarTitle({
            title: "采购结算",
        })
    },
    methods: {
        ...mapMutations(["UP_COMEGO_VIEW"]),
        //获取详情
        getDetail() {
            let path = "?id=" + this.id
            this.isLoading = true
            GetSettlementOrderById(path).then((res) => {
                if (res.State == 1) {
                    this.data = res.Data
                    this.isLoading = false
                    if (this.data.SettlementType == 0) {
                        uni.setNavigationBarTitle({
                            title: "采购结算" + this.data.StateName,
                        })
                    } else {
                        uni.setNavigationBarTitle({
                            title: "退货结算" + this.data.StateName,
                        })
                    }
                    if (this.data.IsCheck == 1 || this.data.IsConfirmPay == 1 || this.data.IsPay == 1) {
                        this.isShowBottomBar = true
                    }
                    if (this.data.FileUrl) {
                        console.log(utils.matchFileSuffixType(this.data.FileUrl))
                        if (utils.matchFileSuffixType(this.data.FileUrl) == "image") {
                            //判断附件是不是图片
                            this.accessoryShow = true
                        } else {
                            this.accessoryShow = false
                        }
                    }
                }
            })
        },
        goUrl(url) {
            this.easyGoTo(url)
        },
        //预览图片
        previewImage(e) {
            var current = e.target.dataset.src
            uni.previewImage({
                current: current,
                urls: this.easyImgList(this.data.FileUrl),
            })
        },
        previewImage1(e) {
            var current = e.target.dataset.src
            uni.previewImage({
                current: current,
                urls: this.easyImgList(this.data.Imgs),
            })
        },
        back() {
            this.$refs.popReturnTip.open()
        },
        agree() {
            this.$refs.popReturnTip1.open()
        },
        close() {
            this.$refs.popReturnTip.close()
        },
        rejectClose() {
            this.$refs.popRejectTip.close()
        },
        confirm() {
            if (this.desc == "") {
                this.easyToast("请填写理由")
                return false
            }
            let path = "?id=" + this.id + "&checkResult=2&remark=" + this.desc
            CheckSettlementOrder(path).then((res) => {
                if (res.State == 1) {
                    this.$refs.popReturnTip.close()
                    this.upView()
                } else {
                    this.easyToast(res.Msg)
                }
            })
        },
        close1() {
            this.$refs.popReturnTip1.close()
        },
        confirm1() {
            let path = "?id=" + this.id + "&checkResult=1"
            CheckSettlementOrder(path).then((res) => {
                if (res.State == 1) {
                    this.$refs.popReturnTip1.close()
                    this.upView()
                } else {
                    this.easyToast(res.Msg)
                }
            })
        },
        DatePicker(type) {
            //显示
            this.type = type
            this.showPicker = true
            this.value = this[type]
        },
        ed(e) {
            //选择
            this.showPicker = false
            if (e) {
                this[this.type] = e.value
                //选择的值
                console.log("value => " + e.value)
                //原始的Date对象
                console.log("date => " + e.date)
                this.payDate = e.value
            }
        },
        chooseImage(index, value) {
            this.upimg = value
            console.log(this.upimg)
        },
        gotolist() {
            if (this.data.SettlementType == 0) {
                this.goUrl(
                    "/pages_supplier/packet/accountBuy/relevanceOrder?data=" +
                        JSON.stringify(this.data.BuyOrderList) +
                        "&type=" +
                        this.data.SettlementType,
                )
            } else {
                this.goUrl(
                    "/pages_supplier/packet/accountBuy/relevanceOrder?data=" +
                        JSON.stringify(this.data.ExchangeList) +
                        "&type=" +
                        this.data.SettlementType,
                )
            }
        },
        payConfirm() {
            let path = "?id=" + this.id + "&checkResult=1"
            ConfirmSettlementOrder(path).then((res) => {
                if (res.State == 1) {
                    this.easyToast("操作成功")
                    this.upView()
                } else {
                    this.easyToast(res.Msg)
                }
            })
        },
        reject() {
            this.$refs.popRejectTip.open()
        },
        submit() {
            let path = "?id=" + this.id + "&payTime=" + this.payDate + "&imgs=" + this.upimg
            PaySettlementOrder(path).then((res) => {
                if (res.State == 1) {
                    this.easyToast("操作成功")
                    this.upView()
                } else {
                    this.easyToast(res.Msg)
                }
            })
        },
        rejectConfirm() {
            if (this.rejectDesc == "") {
                this.easyToast("请填写驳回原因")
                return false
            }
            let path = "?id=" + this.id + "&checkResult=2&remark=" + this.rejectDesc
            ConfirmSettlementOrder(path).then((res) => {
                if (res.State == 1) {
                    this.easyToast("操作成功")
                    this.$refs.popRejectTip.close()
                    this.upView()
                } else {
                    this.easyToast(res.Msg)
                }
            })
        },
        upView() {
            this.isShowBottomBar = false
            this.getDetail()
            this.UP_COMEGO_VIEW(new Date().getTime())
        },
    },
}
</script>

<style lang="scss" scoped>
.container {
    padding: 24rpx;
}
.paddingBottomBar {
    padding-bottom: 130rpx;
}
.pricemain {
    border-radius: 24rpx;
    background: #ffffff;
    margin-bottom: 24rpx;
}
.top {
    padding: 0 24rpx;
    .title {
        font-size: 28rpx;
        color: $color-gray-1;
        padding: 32rpx 24rpx 24rpx 24rpx;
        display: flex;
        align-items: center;
        image {
            width: 32rpx;
            height: 32rpx;
            margin-right: 20rpx;
        }
    }
    .price {
        display: flex;
        justify-content: center;
        align-items: baseline;
        font-size: 64rpx;
        color: $color-gray-1;
        font-weight: bold;
        padding-bottom: 40rpx;
        border-bottom: 2rpx solid #ebebeb;
        view {
            font-size: 40rpx;
            color: $color-gray-1;
            font-weight: bold;
            position: relative;
            top: -4rpx;
        }
    }
}
.footer {
    width: 750rpx;
    height: 120rpx;
    background: #ffffff;
    box-shadow: 0px -2rpx 6rpx 0px rgba(0, 0, 0, 0.1);
    position: fixed;
    left: 0;
    bottom: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    .btn {
        width: 304rpx;
        height: 80rpx;
        margin: 0 23rpx;
    }
}
.row {
    padding: 24rpx;
    & > view {
        display: flex;
        justify-content: flex-start;
        padding: 10rpx 0;
        font-size: 28rpx;
        color: $color-gray-1;
    }
    .row-label {
        color: $color-gray-3;
        width: 140rpx;
    }
    .imglist {
        flex: 1;
        display: flex;
        flex-wrap: wrap;
        margin-bottom: -12rpx;
        image {
            width: 148rpx;
            height: 148rpx;
            margin-right: 24rpx;
            border-radius: 8rpx;
            margin-bottom: 12rpx;
            &:nth-child(3n) {
                margin: 0;
            }
        }
    }
}
.box {
    background: #ffffff;
    border-radius: 8rpx;
    margin-bottom: 24rpx;
    .title {
        font-size: 28rpx;
        color: $color-gray-1;
        height: 88rpx;
        display: flex;
        align-items: center;
        padding: 0 0 0 24rpx;
        font-weight: bold;
        border-bottom: 2rpx solid #ebebeb;
    }
}
.go {
    background: #ffffff;
    border-radius: 8rpx;
    height: 88rpx;
    align-items: center;
    font-size: 28rpx;
    color: $color-gray-1;
    font-weight: bold;
    padding: 0 24rpx;
    margin-bottom: 24rpx;
}
</style>
